
package com.ttomato.game.gamedev2d;

import android.util.FloatMath;

import com.ttomato.game.framework.Game;
import com.ttomato.game.framework.Input.TouchEvent;
import com.ttomato.game.framework.Screen;
import com.ttomato.game.framework.gl.Vertices;
import com.ttomato.game.framework.impl.GLGame;
import com.ttomato.game.framework.impl.GLGraphics;
import com.ttomato.game.framework.math.Vector2;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

public class CannonGravityTest extends GLGame {

    @Override
    public Screen getStartScreen() {
        return new CannonGravityScreen(this);
    }

    class CannonGravityScreen extends Screen {

        static final float FRUSTUM_WIDTH = 9.6f;
        static final float FRUSTUM_HEIGHT = 6.4f;
        GLGraphics mGlGraphics;
        Vertices mCannonVertices;
        Vertices mBallVertices;
        Vector2 mCannonPos = new Vector2();
        float mCannonAngle = 0;
        Vector2 mTouchPos = new Vector2();
        Vector2 mBallPos = new Vector2();
        Vector2 mBallVelocity = new Vector2();
        Vector2 mGravity = new Vector2();

        public CannonGravityScreen(Game game) {
            super(game);
            GLGame glGame = (GLGame) game;
            mGlGraphics = glGame.getGLGraphics();

            mCannonVertices = new Vertices(mGlGraphics, 3, 0, false, false);
            mCannonVertices.setVertices(new float[] {
                    -0.5f, -0.5f,
                    0.5f, 0.0f,
                    -0.5f, 0.5f
            }, 0, 6);

            mBallVertices = new Vertices(mGlGraphics, 4, 6, false, false);
            mBallVertices.setVertices(new float[] {
                    -0.1f, -0.1f,
                    0.1f, -0.1f,
                    0.1f, 0.1f,
                    -0.1f, 0.1f
            }, 0, 8);
            mBallVertices.setIndices(new short[] {
                    0, 1, 2, 2, 3, 0
            }, 0, 6);
        }

        @Override
        public void update(float deltaTime) {
            List<TouchEvent> touchEvents = mGame.getInput().getTouchEvents();
            mGame.getInput().getKeyEvents();

            int len = touchEvents.size();

            for (int i = 0; i < len; i++) {
                TouchEvent event = touchEvents.get(i);

                mTouchPos.mX = (event.x / (float) mGlGraphics.getWidth()) *
                        FRUSTUM_WIDTH;
                mTouchPos.mY = (1 - event.y / (float) mGlGraphics.getHeight()) *
                        FRUSTUM_HEIGHT;
                mCannonAngle = mTouchPos.sub(mCannonPos).angle();

                if (event.type == TouchEvent.TOUCH_UP) {
                    float radians = mCannonAngle * Vector2.TO_RADIANS;
                    float speed = mTouchPos.len();

                    mBallPos.set(mCannonPos);
                    mBallVelocity.mX = FloatMath.cos(radians) * speed;
                    mBallVelocity.mY = FloatMath.sin(radians) * speed;
                }
            }

            mBallVelocity.add(mGravity.mX * deltaTime, mGravity.mY *
                    deltaTime);
            mBallPos.add(mBallVelocity.mX * deltaTime, mBallVelocity.mY *
                    deltaTime);
        }

        @Override
        public void present(float deltaTime) {
            GL10 gl = mGlGraphics.getGl();

            gl.glViewport(0, 0, mGlGraphics.getWidth(),
                    mGlGraphics.getHeight());
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
            gl.glMatrixMode(GL10.GL_PROJECTION);
            gl.glLoadIdentity();
            gl.glOrthof(0, FRUSTUM_WIDTH, 0, FRUSTUM_HEIGHT, 1, -1);
            gl.glMatrixMode(GL10.GL_MODELVIEW);

            gl.glLoadIdentity();
            gl.glTranslatef(mCannonPos.mX, mCannonPos.mY, 0);
            gl.glRotatef(mCannonAngle, 0, 0, 1);
            gl.glColor4f(1, 1, 1, 1);
            mCannonVertices.bind();
            mCannonVertices.draw(GL10.GL_TRIANGLES, 0, 3);
            mCannonVertices.unbind();

            gl.glLoadIdentity();
            gl.glTranslatef(mBallPos.mX, mBallPos.mY, 0);
            gl.glColor4f(1, 0, 0, 1);
            mBallVertices.bind();
            mBallVertices.draw(GL10.GL_TRIANGLES, 0, 6);
            mBallVertices.unbind();
        }

        @Override
        public void pause() {
            // TODO Auto-generated method stub

        }

        @Override
        public void resume() {
            // TODO Auto-generated method stub

        }

        @Override
        public void dispose() {
            // TODO Auto-generated method stub

        }
    }

}
